<?php
/**
 * 员工管理
 *
 * @author 孙晓晔
 * @version $Id: Info.class.php 1035 2010-03-26 05:24:59Z sunxy $s
 */

require_once(dirname(__FILE__) . '/Emp.class.php');

class Info extends Emp {

	public $education = array(
		'-' => '请选择',
		'0' => '小学',
		'1' => '初中',
		'2' => '高中',
		'3' => '中专',
		'4' => '大专',
		'5' => '本科',
		'6' => '硕士',
		
		'9' => '其它',
	);

	public $marriage = array(
		'-' => '请选择',
		'1' => '未婚',
		'2' => '已婚',
		'3' => '离婚',
			
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'name' => '姓名',
		'birthday' => '年龄',
		'sex' => '性别',
		'education' => '学历',
		'marriage' => '婚姻',

		'indate' => '有效期',

		'province' => '省份',
		'city' => '城市',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'dept' => '部门',
		'sex' => '性别',
		'nationality' => '民族',
		'education' => '学历',
		'marriage' => '婚姻',

		'province' => '省',
		'city' => '市',
		'district' => '县',

		'status' => '状态',

		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:35:48 $';
		$request['revision'] = '$Revision: 1.2 $';

		parent::__construct($request);
	}

	/**
	 * 预处理
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('education_option', $this->education);

		$controller->assign_by_ref('marriage_option', $this->marriage);

		$this->nationality = $this->code_option("1");
		$controller->assign_by_ref('nationality_option', $this->nationality);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'emp/info/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$code_s = $request['code_s'];
		$name_s = $request['name_s'];

		$idcard_s = $request['idcard_s'];

		$book_s = $request['book_s'];
		$mobile_s = $request['mobile_s'];

		$sex_s = $request['sex_s'];
		$nationality_s = $request['nationality_s'];
		$marriage_s = $request['marriage_s'];
		$education_s = $request['education_s'];

		$province_s = $request['province_s'];
		$city_s = $request['city_s'];

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($code_s)) {
			$where .= " AND code LIKE '" . $code_s . "%'";
		}

		if (!empty($name_s)) {
			$where .= " AND name LIKE '" . $name_s . "%'";
		}

		if (!empty($idcard_s)) {
			$where .= " AND idcard LIKE '%" . $idcard_s . "%'";
		}

		if (!empty($book_s) && $book_s <> '-') {
			$where .= " AND book = '" . $book_s . "'";
		}

		if (!empty($mobile_s)) {
			$where .= " AND mobile like '%" . $mobile_s . "%'";
		}

		if (!empty($province_s)) {
			$where .= " AND province LIKE '" . $province_s . "%'";
		}

		if (!empty($city_s)) {
			$where .= " AND city LIKE '" . $city_s . "%'";
		}

		if (!empty($sex_s) && $sex_s <> '-') {
			$where .= " AND sex = '" . $sex_s . "'";
		}

		if (!empty($nationality_s) && $nationality_s <> '-') {
			$where .= " AND nationality = '" . $nationality_s . "'";
		}

		if (!empty($marriage_s) && $marriage_s <> '-') {
			$where .= " AND marriage = '" . $marriage_s . "'";
		}

		if (!empty($education_s) && $education_s <> '-') {
			$where .= " AND education = " . $education_s;
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
				'db' => $db,
				'sql' => $sql,
				'PageSize' => $this->pagesize,
				'CurrentPageID' => $request['p'],
				'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'emp/info/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/info/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$sex = $request['sex'];

		$birthday = $request['birthday'];
		$idcard = $request['idcard'];
		$indate = $request['indate'];
		$fzjg = addslashes($request['fzjg']);

		$nationality = $request['nationality'];
		$education = $request['education'];

		$marriage = $request['marriage'];
		$address = addslashes($request['address']);
		$province = $request['province'];
		$city = $request['city'];
		$district = $request['district'];

		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET sex = '%s', birthday = '%s', idcard = '%s', indate = '%s', fzjg = '%s', nationality = '%s', education = '%s'";
		$fmt .= ", marriage = '%s', address = '%s', province = '%s', city = '%s', district = '%s', content = concat(content, '%s'), modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $sex, $birthday, $idcard, $indate, $fzjg, $nationality, $education, $marriage, $address, $province, $city, $district, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function upload($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
		
		$row['files'] = $this->_files($row['id']);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/info/upload');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$ret = $this->_view_tab();

		$controller->assign_by_ref('tabs', $ret);

		$controller->display($request, 'emp/info/view');
		return true;
	}

	function view_emp($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['photo_t'] = $this->_photo($row);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/info/_view');
		return true;
	}

	function _my_bank($tbl, $id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE info = 1 AND info_id = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $tbl, $id);
		// debug($sql);
		$result = $db->query($sql);

		return $db->fetch_all($result);
	}

	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}

		$row['education_t'] = $this->education[$row['education']];
		$row['marriage_t'] = $this->marriage[$row['marriage']];
		$row['nationality_t'] = $this->nationality[$row['nationality']];

		// $row['d1'] = $this->date_diff('d', $row['join'], date("Y-m-d"));
		// $row['d1'] = $this->date_1($row['join'], date("Y-m-d"));
		// $row['d2'] = $this->date_1($row['join'], $row['leave']);

		$row = parent::_view($id, $row);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-100 day'));
		// $controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'emp/info/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'dept');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'dept') {
			$row['a'] = Name::dept($row['a']);
		} elseif ($px == 'sex') {
			$row['a'] = $this->sex[$row['a']];
		} elseif ($px == 'nationality') {
			$row['a'] = $this->nationality[$row['a']];
		} elseif ($px == 'education') {
			$row['a'] = $this->education[$row['a']];
		} elseif ($px == 'marriage') {
			$row['a'] = $this->marriage[$row['a']];
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function ajax_upload($controller, $request) {
		$s = print_r($request, true);
		debug($s);

		require_once 'HTTP/Upload.php';
		$upload = new HTTP_Upload('en');
		$file = $upload->getFiles('Filedata');
		if (PEAR::isError($file)) {
			die($file->getMessage());
		}

		// debug(print_r($file, true));

		if ($file->isValid()) {
			$now = date('Y-m-d H:i:s');

			$id = $request['id'];
			$dir = Download::dir2($this->info_dir, $id);
			// debug($dir);
			if (!@file_exists($dir)) {
				umask();
				mkdir($dir, 0777, true);
			}

			$file->setName('uniq');
			$filename = $file->moveTo($dir);
			if (PEAR::isError($filename)){
				die($filename->getMessage());
			}

			$filesize = $file->upload['size'];

			$db = newdb($this->dsn_w);
			$fmt = "UPDATE %s SET photo = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $filename, $id);
			// debug($sql);
			$db->query($sql);
				
			$mg = newfs('ydt');
				
			$key = sprintf("emp_%s", $id);
			try {
				debug($dir . $filename);
					
				$mg->put($dir . $filename, $key, 'upload');
				$mg->close();
			} catch(MogileFsException $e) {
				debug("error");
			}
		}

		echo "1";
		return true;
	}

	function ajax_idcard($controller, $request) {
		$idcard = $request['idcard'];

		if (strlen($idcard) == 15 || strlen($idcard) == 18) {
			$identity = substr($idcard, 0, 6);
			$ret = $this->_idcard_region($identity);
				
			$len = strlen($idcard);
			if ($len == 15) {
				return $this->_idcard_15($ret, $idcard);
			}
				
			if ($len == 18) {
				return $this->_idcard_18($ret, $idcard);
			}
		}

		return false;
	}

	function _idcard_region($identity) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT province, city, region FROM %s WHERE identity = '%s'";
		$sql = sprintf($fmt, "fy.region", $identity);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return $db->fetch_array($result);
		}

		return false;
	}

	function _idcard_15($ret, $idcard) {
		$a = substr($idcard, 14, 1);
		$b = "19" . substr($idcard, 6, 6);

		$c = fmod($a, 2);
		$ret['sex'] = $c > 0 ? 1 : 2;
		$ret['birthday'] = date("Y-m-d", strtotime($b));

		echo json_encode($ret);

		return $ret;
	}

	function _idcard_18($ret, $idcard) {
		$a = substr($idcard, 16, 1);
		$b = substr($idcard, 6, 8);

		$c = fmod($a, 2);
		$ret['sex'] = $c > 0 ? 1 : 2;
		$ret['birthday'] = date("Y-m-d", strtotime($b));

		echo json_encode($ret);

		return $ret;
	}

	function _list($controller, $id) {
		$db = newdb($this->dsn_r);
		$data = array();
		$fmt = "SELECT * FROM %s WHERE status != '3' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl);
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$row['status_t'] = $this->status[$row['status']];
				
			if ($this->_emp_exam($row['id'], $id)) {
				$row['class'] = "class='added'";
				$data[] = $row;
			}

			$data_all[] = $row;
		}

		$controller->assign_by_ref('emp', $data);
		$controller->assign_by_ref('emp_all', $data_all);
	}

	function _emp_exam($emp, $exam) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1' AND emp = '%s' AND exam = '%s'";
		$sql = sprintf($fmt, Constant::tbl_exam_emp, $emp, $exam);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($row['status'] == '1') {
				return true;
			}
		}

		return false;
	}
	
	function t($controller, $request) {
		
		$filename = "http://192.168.16.13:7500/dev202/0/000/001/0000001942.fid";
		// $filename = "/Users/sunxiaoye/Pictures/zhangm_1.jpg";
		
		echo $filename . "\n";
		
		require_once(dirname(__FILE__) . '/../core/FileType.class.php');
		
		echo FileType::getFileType($filename);
		
	
		logdate($controller, $request);
		return true;
	}
}
?>
